//
//  PPUserCenterMainPageTableViewController.m
//  PetParadise
//
//  Created by 陈无忌 on 8/2/14.
//  Copyright (c) 2014 whalefin. All rights reserved.
//

#import "PPUserCenterMainPageTableViewController.h"
//systemSetting
#import "supportingHeadFiles.h"
#import "PPSystemConfiguraton.h"
//tabelViewCell
#import "PPTopicTableViewCell.h"
//collectionViewCell
#import "PPPetImageCell.h"
//ImageDisplayView
#import "PPImageShowView.h"
//connectViewController
#import "PPAddTopicViewController.h"
#import "PPPetDetailTableViewController.h"
#import "PPSelfInfoTableViewController.h"
#import "PPTopicDetailViewController.h"
#import "PPAddPetTableViewController.h"
//class
#import "PPPetInfo.h"
#import "PPTopic.h"




@interface PPUserCenterMainPageTableViewController ()<UIGestureRecognizerDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *headView;
//headView
@property (weak, nonatomic) IBOutlet UICollectionView *petCollectionView;
@property (weak, nonatomic) IBOutlet UIImageView *headViewImageView;

@property (weak, nonatomic) IBOutlet UIImageView *userAvatatImageView;
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *cityLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailInfoLabel;

@property (strong, nonatomic) NSMutableArray *petArray;
@property (strong, nonatomic) NSMutableArray *topicArray;
@property (strong, nonatomic) PPUserInfo *user;

@property (weak, nonatomic) PPSystemConfiguraton *config;

@end

@implementation PPUserCenterMainPageTableViewController
-(PPSystemConfiguraton *)config
{
    if (!_config) {
        _config = [PPSystemConfiguraton sharedSystemConfiguration];
    }
    return _config;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    //获得话题列表
    self.topicArray = [self.config.topicArray mutableCopy];
    [self.tableView reloadData];
    //获得宠物列表
    self.petArray = [self.config.petsArray mutableCopy];
    [self.petCollectionView reloadData];
    //获取用户
    self.user = self.config.userInfo;
    self.detailInfoLabel.text = [NSString stringWithFormat:@"话题：%d / 粉丝：%d / 关注：%d ",self.topicArray.count,self.config.userInfo.fansNum,self.config.userInfo.followNum];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.hidesBackButton = NO;
    //set data
    self.petCollectionView.delegate = self;
    self.petCollectionView.dataSource = self;


    [self headViewLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(operateNotification:) name:@"reloadTopic" object:nil];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)operateNotification:(NSNotification *)notification
{
    NSLog(@"recive notification");
    NSDictionary *dic = [notification userInfo];
    NSNumber *result = dic[@"result"];
    if (result.intValue == 0) {
        [PPHUD hudShowWithString:@"发布失败" andView:self.view];
    }else if (result.intValue == 1){
        [self.config getTopicListwithComepletion:^{
            [PPHUD hudShowWithString:@"发布成功" andView:self.view];
            self.topicArray = [self.config.topicArray mutableCopy];
            [self.tableView reloadData];
            self.detailInfoLabel.text = [NSString stringWithFormat:@"话题：%d / 粉丝：%d / 关注：%d ",self.topicArray.count,self.config.userInfo.fansNum,self.config.userInfo.followNum];
        }];
    }else if (result.intValue == 2){
        [PPHUD hudShowWithString:@"网络不给力，发布失败" andView:self.view];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - dataLoad
-(void)headViewLoad
{
    [self.headViewImageView setContentMode:UIViewContentModeScaleAspectFill];
    [self.headViewImageView setClipsToBounds:YES];
    [self.headViewImageView setImage:[UIImage imageNamed:@"banner"]];
    //userAvatar
    [self.userAvatatImageView setImageWithURL:[PPImageUrl getImageUrl:self.config.userInfo.userAvatar] placeholderImage:[UIImage imageNamed:@"defaultAvatar"] usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    //gesture
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goInfo)];
    self.userAvatatImageView.userInteractionEnabled = YES;
    recognizer.numberOfTouchesRequired = 1;
    recognizer.numberOfTapsRequired = 1;
    [self.userAvatatImageView addGestureRecognizer:recognizer];
    //static view
    self.userNameLabel.text = self.config.userInfo.userName;
    self.cityLabel.text = self.config.userInfo.address;
}

#pragma mark - ViewController Change

-(void)goInfo
{
    PPSelfInfoTableViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PPSelfInfoTableViewController"];
    [self.navigationController pushViewController:targetVC animated:YES];
}


#pragma mark - Table view data source

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.topicArray.count > 0) {
        PPTopic *topic = [self.topicArray objectAtIndex:indexPath.row];
        return [PPTopicTableViewCell getCellHeight:topic];
    }else{
        return 0;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.topicArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PPTopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topicCell"];
    PPTopic *topic = [self.topicArray objectAtIndex:indexPath.row];
    if (![topic isEqual:self.topicArray.firstObject]) {
        PPTopic *beforeTopic = [self.topicArray objectAtIndex:indexPath.row - 1];
        [cell cellInitWithTopic:topic andBeforeTopic:beforeTopic];
    }else{
        [cell cellInitWithTopic:topic andBeforeTopic:nil];
    }
    return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    PPTopicDetailViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PPTopicDetailViewController"];
    targetVC.topic = [self.topicArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:targetVC animated:YES];
}

#pragma mark - Collection

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.petArray.count) {
        return self.petArray.count + 1;
    }
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PPPetImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"petImageCell" forIndexPath:indexPath];
    if (indexPath.row == self.petArray.count) {
        [cell.petAvatarImageView setImage:[UIImage imageNamed:@"setPet"]];
    }else{
        PPPetInfo *pet = [self.petArray objectAtIndex:indexPath.row];
        [cell.petAvatarImageView setImageWithURL:[PPImageUrl getImageUrl:pet.petAvatar] placeholderImage:[UIImage imageNamed:@"defaultAvatar"] usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.petArray.count) {
        PPAddPetTableViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PPAddPetTableViewController"];
        [self.navigationController pushViewController:targetVC animated:YES];
    }else{
        PPPetInfo *pet = [self.petArray objectAtIndex:indexPath.row];
        PPPetDetailTableViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PPPetDetailTableViewController"];
        targetVC.pet = pet;
        [self.navigationController pushViewController:targetVC animated:YES];
    }
    
}


@end
